package com.alibaba.org.controller;

import com.alibaba.basic.uitl.AjaxResult;
import com.alibaba.basic.uitl.PageList;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.query.EmployeeQuery;
import com.alibaba.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emp")
//@Controller
@RestController // @Controller + @ResponseBody
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * restful、 风格
     */
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }


    /**
     * restful 风格 路径变量
     * 如果前端传的参数名称 叫 deptId
     * http://localhost:8080/dept/1
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee findById(@ApiParam(value = "ID属性", required = true)
                             @PathVariable("id") Long id) {
        return employeeService.finById(id);
    }

    /**
     * 添加/ 修改
     *
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (null == employee.getId()) {
            // 添加
            employeeService.add(employee);
        } else {
            // 编辑
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页 json格式交互 字符串
     *
     * @RequestBody 接收前端的json数据 转成我们的实体
     */
    @PostMapping
//    @PreAuthorize(name = "员工列表",sn = "employee:list")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {
//        UserInfo userInfo = LoginContext.getUserInfo();
//        if (null != userInfo.getShop()) {
//            quey.setShopId(userInfo.getShop().getId());
//        }
        return employeeService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDetel(@RequestBody List<Long> ids) {
        employeeService.deleteId(ids);
        return AjaxResult.me().success();
    }



    /**
     * 查询店铺下的员工
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> findListByShopId(@PathVariable("shopId") Long shopId) {
        return employeeService.findListByShopId(shopId);
    }



    /**
     * 根据部门id查询员工列表数据
     */
    @GetMapping("/findEmpByDeptId/{id}")
    public List<Employee> findEmpByDeptId(@PathVariable("id") Long id) {
        return  employeeService.findEmpByDeptId(id);
    }
}
